<template>
  <div>
    <!--        面包屑导航-->
    <breadcrumb :navData="navData"></breadcrumb>
    <!-- card -->
    <el-card class="box-card">
      <header>
        <el-row :gutter="13">
          <el-col :span="10">
            <el-input
              placeholder="请输入内容"
              class="input-with-select"
              v-model="store_msg.query"
              clearable
              @clear="getstore_list"
            >
              <el-button slot="append" icon="el-icon-search" @click="getstore_list"></el-button>
            </el-input>
          </el-col>
          <el-col :span="3">
            <div class="grid-content bg-purple">
              <el-button type="primary" @click="add_router">添加用户</el-button>
            </div>
          </el-col>
        </el-row>
      </header>
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="#" width="50" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="140px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="140px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="190px">
          <template slot-scope="scope">
            <!--                        自定义过滤器-->
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <!-- template -->
          <template slot-scope="scope">
            <!-- 编辑用户 -->
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"></el-button>
            <!-- 删除 -->
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="del(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="store_msg.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="store_msg.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "VueShopsGoods",

  data() {
    return {
      navData: ["商品管理", "商品列表"],
      tableData: [],
      store_msg: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      // 添加弹出
      dialogFormVisible: false,
      total: 0,
    };
  },

  mounted() {},
  created() {
    this.getstore_list();
  },
  methods: {
    // 表格数据
    async getstore_list() {
      const store = await this.axios.get("/goods", {
        params: this.store_msg,
      });
      console.log(store);
      this.tableData = store.data.data.goods;
      this.total = store.data.data.total;
    },
    // 每页几条
    handleSizeChange(val) {
      this.store_msg.pagesize = val;
      this.getstore_list();
    },
    // 哪一页
    handleCurrentChange(val) {
      this.store_msg.pagenum = val;
      this.getstore_list();
    },
    // 添加确定
    add_router() {
      this.$router.push("/add");
    },
    // 编辑点击
    edit(row) {},
    // 删除点击
    async del(row) {
      console.log(row);
      const del_data = await this.axios.delete(`goods/${row.goods_id}`);
      console.log(del_data);
      if (del_data.data.meta.status == 200) {
        this.$message.success("删除成功");
        this.getstore_list();
      } else {
        this.$message.error("删除失败");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}
</style>